package util.base;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 
 * @author hxl
 *
 */
public class VerifyCodeUtil {
	 private String defaultCodes="1234567890abcdefghijknmopqrstuvwxyz";
	 Random random = new Random();
	/**
	 * 获得一个验证码图片
	 * @param os 输出流
	 * @param width  宽
	 * @param heigth 高
	 * @param codes  验证码字符范围
	 * @param codeLength  验证码字符个数
	 * @param interferingLineNumber 干扰线个数
	 * @param font 字体
	 * @return
	 */
     public  String getImages(OutputStream os,int width,
    		 int heigth,
    		 String codes,
    		 int codeLength,
    		 int interferingLineNumber,
    		 Font font){
    	 
    	 if(null==codes||"".equals(codes)){
    		 codes=this.defaultCodes;
    	 }
    	 BufferedImage images=new BufferedImage(width,heigth,BufferedImage.TYPE_INT_RGB);
         Graphics2D g2 = images.createGraphics();
         //渲染设置
         g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
         //设置边框色为白色
         g2.setColor(Color.white);
         g2.fillRect(0, 0,width,heigth);
         
         //设置干扰线
         for (int i = 0; i <interferingLineNumber; i++) {
 			int x =1;
 			int y = random.nextInt(font.getSize())+(heigth-font.getSize())/2;
 			int x1 = width-1;
 			int y1 =random.nextInt(font.getSize())+(heigth-font.getSize())/2;
 			g2.setColor(getRandColor());
 			g2.drawLine(x, y,x1,y1);
 		  }
        /*// 添加噪点
 		float yawpRate = 0.05f;// 噪声率
 		int area = (int) (yawpRate * w * h);
 		for (int i = 0; i < area; i++) {
 			int x = random.nextInt(width);
 			int y = random.nextInt(heigth);
 			images.setRGB(x, y, getRandColor());
 		}*/
         //设置字体
         g2.setFont(font);
         
         //写入随机字符
         char[] chars=getChars(codes,codeLength);
         for(int i=0;i<chars.length;i++){
        	 g2.setColor(getRandColor());
  			 g2.drawString(String.valueOf(chars[i]), (width/codeLength)*i,(heigth-font.getSize())/2+font.getSize());
         }
         g2.dispose();
         try {
			ImageIO.write(images,"jpg",os);
		} catch (IOException e) {
			e.printStackTrace();
		}
         return new String(chars);
     };
    /**
     * 获得随机字符
     * @param code
     * @param codeLength
     * @return
     */
    private char[] getChars(String code,int codeLength){
    	  char[] chars=new char[codeLength];
    	  for(int i=0;i<codeLength;i++){
    		  chars[i]=code.charAt(random.nextInt(code.length()));
    	  }
    	return chars;
    }
    /**
     * 生成随机颜色
     * @return
     */
    private  Color getRandColor() {
    	int r = (int)(Math.random()*255)+1;
    	int g = (int)(Math.random()*255)+1;
    	int b = (int)(Math.random()*255)+1;
 		return new Color(r, g, b);
 	}
    
    public void write(BufferedImage bufferedImage, String target) throws IOException {
        File file = new File(target);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try (OutputStream os = new FileOutputStream(target)) {
            ImageIO.write(bufferedImage, "PNG", os);
        }
    }
}
